package com.yzf.demo.demo.Thread;

/**
 * Created by 于占峰 on 2020/3/31/031.
 */
public class InterruptDemo {

    private static int i;
    //使用Thread.currentThread().isInterrupted()中断线程
//    public static void main(String[] args) {
//        Thread thread = new Thread(()->{
//            //Thread.currentThread().isInterrupted()默认是false
//            while (!Thread.currentThread().isInterrupted()){
//                i++;
//            }
//        });
//        thread.start();
//        //将false改变为true
//        thread.interrupt();
//    }

    /**
     * 线程结束意味着run方法执行结束
     * 当线程处于阻塞状态 无法终止时 可以调用interrupt方法
     * 线程会从阻塞状态唤醒并抛出InterruptedException异常
     * 然后结束当前的线程
     *
     * @param args
     */

    public static void main(String[] args) {
        Thread thread = new Thread(() -> {
            try {
                Thread.sleep(100000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        thread.start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        thread.interrupt();
    }
}
